#ifndef BASEINI_H
#define BASEINI_H

#include <QFile>
#include <QDebug>
#include <QSettings>
#include <QMutex>

template <class T>

class BaseIni{

public:
    QString path() const
    {
        return m_path;
    }
    void setPath(const QString &path)
    {
        m_path = path;
    }

    static T* getInstance()
    {
        if(!m_pInstance)
        {
            QMutexLocker lock(&m_Mutex);
            if(!m_pInstance)
            {
                T *pInstance = new T();  // 修改处
                m_pInstance = pInstance;
            }
        }
        return m_pInstance;
    }

protected:

    BaseIni(){}
    virtual ~BaseIni(){}

    QString m_path;

    bool check()
    {
        QFile file(m_path);
        if(!file.open(QFile::ReadOnly))
        {
            qDebug()<< "Error:" + m_path + "\n" +
                       "ErrorMessage" + file.errorString() + "\n";
            file.close();
            return false;
        }
        file.close();
        return true;
    }

    bool clearAllNode()
    {
        if(!this->check())
        {
            return false;
        }

        QFile file(m_path);
        file.open(QFile::WriteOnly|QFile::Truncate);
        file.flush();
        file.close();
        return true;
    }

    virtual bool parser() = 0;
    virtual void clear() = 0;

private:

    static T* m_pInstance;
    static QMutex m_Mutex;
    BaseIni(const BaseIni &);
    BaseIni &operator=(const BaseIni &);

};

template <class T>
T* BaseIni<T>::m_pInstance = NULL;
template <class T>
QMutex BaseIni<T>::m_Mutex;

#endif // BASICINI_H
